<template>
  <div class="filter-content pc-filter">
    <template v-if="filterList && filterList.length">
      <div class="filter-content-item" v-for="(item, index) in filterList" :key="index">
        <div class="filter-item">
          <div class="label">{{ item.label }}:</div>
          <div class="filter-check">
            <template v-for="(item2, index2) in item.list" :key="index2">
              <a-checkbox v-model:checked="item2.checked" v-if="item2.title">{{
                item2.title
              }}</a-checkbox>
            </template>
          </div>
        </div>
      </div>
      <div class="filter-content-item">
        <div class="filter-item">
          <div class="label">{{ $t("filters.search") }}：</div>
          <div class="filter-search">
            <a-input v-model:value="inputValue" @pressEnter="inputSearch">
              <template #suffix><search-outlined color="#172026" /></template>
            </a-input>
          </div>
        </div>
      </div>
      <div
        v-if="!hideLine"
        class="divider-line"
        style="margin-top: 32px; margin-bottom: 64px"
      ></div>
    </template>
    <template v-else>
      <div class="pc-skeleton">
        <div class="pc-skeleton-inner">
          <div class="row" v-for="i in 6" :key="i">
            <div class="label">
              <Skeleton height="100%" width="100%" round></Skeleton>
            </div>
            <div class="content">
              <Skeleton height="100%" width="100%"></Skeleton>
            </div>
          </div>
        </div>
        <div
          v-if="!hideLine"
          class="divider-line"
          style="margin-top: 32px; margin-bottom: 64px"
        ></div>
      </div>
    </template>
  </div>
  <div class="mobile-filter">
    <template v-if="filterList && filterList.length">
      <div class="search-box">
        <a-input
          v-model:value.lazy="inputValue"
          @pressEnter="inputSearch"
          :placeholder="$t('placeholder.filter_keyword')"
        >
          <template #suffix><search-outlined color="#172026" /></template>
        </a-input>
        <div
          class="filter-control"
          @click="mobileFilterVisible = !mobileFilterVisible"
          :class="mobileFilterVisible ? 'active' : ''"
        >
          <svg viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
            <path
              d="M13.3128 1.99935L2.68726 1.99935C2.13414 1.99935 1.8216 2.63408 2.15884 3.07249L5.36176 7.23629C5.4514 7.35283 5.50001 7.49573 5.50001 7.64276L5.50001 12.2147C5.50001 12.4873 5.66597 12.7324 5.91908 12.8336L9.58574 14.3003C10.0237 14.4755 10.5 14.153 10.5 13.6813L10.5 7.64276C10.5 7.49573 10.5486 7.35283 10.6383 7.23629L13.8412 3.07249C14.1784 2.63408 13.8659 1.99935 13.3128 1.99935Z"
              stroke="#172026"
            />
          </svg>
          <span>{{ $t("btnText.filter") }}</span>
        </div>
      </div>
      <div class="mobile-filter-container">
        <Transition name="fadeIn">
          <div
            class="mobile-filter-mask"
            @click="mobileFilterVisible = false"
            v-show="mobileFilterVisible"
          ></div>
        </Transition>
        <Transition name="slideDown">
          <div class="mobile-filter-content" v-show="mobileFilterVisible">
            <div class="mobile-filter-check-container">
              <template v-for="item in filterList" :key="item.label">
                <div class="mobile-filter-item" v-if="item.type !== 'input'">
                  <div class="title">
                    {{ item.label }}
                  </div>
                  <div class="filter-check-list">
                    <div
                      class="filter-check-item"
                      :class="item2?.checked ? 'active' : ''"
                      v-for="(item2, index) in item.list"
                      :key="index"
                      @click="onClickFilterItem(item2, index)"
                    >
                      {{ item2.title }}
                    </div>
                  </div>
                </div>
              </template>
            </div>
            <div class="mobile-filter-btns">
              <a-button type="default" @click="onClearFilter">{{
                $t("btnText.clear_filter")
              }}</a-button>
              <a-button type="primary" @click="onCompleteFilter">{{
                $t("btnText.complete_filter")
              }}</a-button>
            </div>
          </div>
        </Transition>
      </div>
    </template>

    <template v-else>
      <div class="mobile-skeleton">
        <div class="mobile-skeleton-inner">
          <Skeleton width="1388px" radius="51px" height="205px"></Skeleton>
          <Skeleton width="250px" height="205px" radius="51px"></Skeleton>
        </div>
      </div>
    </template>
  </div>
</template>

<script setup lang="ts">
type checkBoxItem = {
  label: string;
  value: string;
  color?: string;
  checked?: boolean;
};

type FilterItem = {
  label?: string;
  value?: string[] | string;
  type?: string;
  list?: checkBoxItem[];
};

type Props = {
  filterList?: FilterItem[];
  hideLine?: boolean;
};
const props = withDefaults(defineProps<Props>(), {
  hideLine: false,
});

// 搜索框
const inputValue = ref("");
const emits = defineEmits(["inputSearch", "completeFilter"]);
const inputSearch = () => {
  emits("inputSearch", inputValue.value);
};

// 点击筛选选项
const onClickFilterItem = (item: checkBoxItem, index: number) => {
  if (typeof item?.checked == "undefined") {
    item.checked = true;
  } else {
    item.checked = !item.checked;
  }
};

const onClearFilter = () => {
  inputValue.value = "";
  props.filterList.map((item: FilterItem) => {
    item.list?.map((item: checkBoxItem) => {
      item.checked = false;
    });
  });
  emits("clearFilter");
};

const onCompleteFilter = () => {
  mobileFilterVisible.value = false;
  emits("completeFilter");
};

// mobile filter visible
const mobileFilterVisible = ref(false);
</script>
<style scoped lang="less">
:deep(.ant-checkbox-group) {
  .ant-checkbox-wrapper {
    align-items: center;
  }
  .ant-checkbox-inner {
    width: 16px !important;
    height: 16px !important;
    border-radius: 4px !important;
    &::after {
      width: 5.7142857142857135px !important;
      height: 9.142857142857142px !important;
      border-width: 2px !important;
    }
  }
  .ant-checkbox + span {
    padding: 0 8px !important;
  }
  span {
    font-size: 14px !important;
  }
}
:deep(.ant-input-affix-wrapper) {
  width: 320px;
  height: 40px;
  font-size: 14px;
  padding: 4px 11px;
  border-radius: 6px;
}
.pc-filter {
  .filter-check {
    :deep(.ant-checkbox-inner) {
      width: 16px;
      height: 16px;
      border-radius: 4px;
      &::after {
        width: 5.7142857142857135px;
        height: 9.142857142857142px;
        border-width: 2px;
      }
    }
    :deep(.ant-checkbox-wrapper) {
      display: flex;
      align-items: center;
      & > span:last-child {
        font-size: 16px;
        padding: 0 8px;
      }
    }
  }
}
.filter-content {
  display: flex;
  flex-direction: column;
  gap: 24px;
}
.filter-item {
  display: flex;
  &:last-child {
    align-items: center;
    padding-top: 0;
  }
  .label {
    padding-top: 4px;
    font-weight: 600;
    font-size: 14px;
    white-space: nowrap;
    color: #172026;
    margin-right: var(--yd-text-gap);
  }
  .filter-check {
    display: flex;
    flex-wrap: wrap;
    gap: var(--yd-text-gap);
    :deep(.v-input__details) {
      display: none;
    }
  }
  .filter-search {
    margin-left: 5px;
    display: flex;
    align-items: center;
  }
}
.mobile-filter {
  display: none;
}

// skeleton
.pc-skeleton-inner {
  display: flex;
  gap: 24px;
  flex-direction: column;
  .row {
    display: flex;
    gap: 24px;
  }
  .label {
    width: 70px;
    height: 30px;
  }
  .content {
    flex: 1;
  }
}

.mobile-skeleton-inner {
  display: flex;
  padding: 82px 82px 41px;
  margin-bottom: 82px;
  background-color: #fff;
  justify-content: space-between;
}

@media screen and (max-width: 768px) {
  .pc-filter {
    display: none;
  }
  .mobile-filter {
    display: block;
    position: sticky;
    top: var(--yd-header-height);
    left: 0;
    z-index: 99;
    margin-left: -82px;
    margin-right: -82px;
    .search-box {
      padding: 82px 82px 41px;
      height: 328px;
      background-color: #fff;
      margin-bottom: 82px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      :deep(.ant-input-affix-wrapper) {
        width: 1388px;
        height: 205px;
        font-size: 72px;
        padding: 51px 41px;
        border-radius: 51px;
      }
      .filter-control {
        display: flex;
        align-items: center;
        gap: 20px;
        font-size: 72px;
        &.active {
          color: #3d88f2;
          svg path {
            stroke: #3d88f2;
          }
        }
        svg {
          width: 82px;
          height: 82px;
        }
      }
    }
    .mobile-filter-container {
      pointer-events: none;
      z-index: -1;
      position: absolute;
      inset: 0;
      top: 328px;
      .mobile-filter-mask {
        position: fixed;
        inset: 0;
        top: 0;
        left: 0;
        background: rgba(0, 0, 0, 0.6);
        pointer-events: auto;
      }
      .mobile-filter-content {
        position: relative;
        z-index: 9;
        height: 2355px;
        background-color: #fff;
        border-bottom-left-radius: 82px;
        border-bottom-right-radius: 82px;
        padding: 82px 0;
        pointer-events: auto;
      }
      .mobile-filter-check-container {
        height: 1900px;
        overflow-y: auto;
        padding: 0 82px;
      }
      .mobile-filter-item {
        margin-bottom: 164px;
        color: #36434d;
        .title {
          margin-bottom: 82px;
          color: #172026;
        }
        .filter-check-list {
          display: grid;
          grid-template-columns: repeat(3, 1fr);
          gap: 67px;
          .filter-check-item {
            text-align: center;
            padding: 51px 30px;
            font-size: 72px;
            border-radius: 51px;
            border: 3px solid #e6edf2;
            display: flex;
            justify-content: center;
            align-items: center;
            &.active {
              border: 3px solid #3d88f2;
              background: #ecf3fe;
              color: #3d88f2;
            }
          }
        }
      }
      :deep(.mobile-filter-btns) {
        height: 369px;
        display: flex;
        align-items: center;
        border-top: 3px solid #e6edf2;
        padding: 0 82px;
        .ant-btn {
          height: 246px !important;
          width: 573px !important;
          border-radius: 61px !important;
          font-size: 82px !important;
        }
        .ant-btn:first-child {
          background: #e6edf2;
          border-color: transparent;
          color: #36434d;
        }
        .ant-btn:last-child {
          width: 1121px !important;
          margin-left: 61px;
        }
      }
    }
  }
}
</style>
